Okay, I'll look into creating a nice Class before my code gets really hectic.
Also, I've tried altering my code so that instead of constantly creating and destroying HDCs, BITMAPS, etc. for every repaint they last the entire life cycle of my program and are initialized early, but my program is eating up the CPU like 96% now. If I acquire a device context at the birth of my program (i.e. init() function) and do not release it until the end of my program (i.e. freeResources()) is this what is causing my app. to run at 96% CPU?
Also, I had hoped SetDimensionsEx() would resize the bitmap buffer later, but there turns out to be no function capable of this.
My altered code from my previous code posted earlier:
Globals
Code:
HDC screenHDC;
HDC hdcBuffer;
HBITMAP bmpBuffer;
HBRUSH backgroundBrush; // Brush used for painting the background.
RECT rect;
Code:
case WM_PAINT:
GetClientRect(windowHandle, &rect);
// Repaint the window with the background brush defined above.
FillRect(hdcBuffer, &rect, backgroundBrush);
deck.redrawAll(hdcBuffer, rect); // Redraws all relevant cards to the screen.
// Copy back buffer into screen device context for display.
BitBlt(screenHDC, 0, 0, rect.right, rect.bottom, hdcBuffer, 0, 0, SRCCOPY);
break;
case WM_DESTROY:
freeResources(windowHandle);
PostQuitMessage(0);
break;
Code:
bool initialize(HWND windowHandle)
{
screenHDC = GetDC(windowHandle);
GetClientRect(windowHandle, &rect);
hdcBuffer = CreateCompatibleDC(screenHDC);
// Forced to define a large size 1280x1024 because impossible to resize bmpBuffer later.
bmpBuffer = CreateCompatibleBitmap(screenHDC, 1280, 1024);
SelectObject(hdcBuffer, bmpBuffer);
backgroundBrush = CreateSolidBrush(RGB(0,155,0));
return true;
}
void freeResources(HWND windowHandle)
{
ReleaseDC(windowHandle, screenHDC);
DeleteDC(hdcBuffer); // Free resources.
DeleteObject(bmpBuffer); // Free resources.
// Delete brush created earlier.
DeleteObject(backgroundBrush);
}